package admin;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import test.InputHandler;
import test.LocalProperties;


import com.objective.oji.OjiException;

/**
 * 
 * @author sonseo
 *
 * DATE FORMAT "29-1-2006 22:01:01" or "29-1-2006"
 */
class AdminTools {

	final static String myAppPROPERTIES_FILE = "C:\\dev\\profile\\AdminTools.properties";
	static LocalProperties myAppProperty = new LocalProperties(myAppPROPERTIES_FILE);
	static SERVER_TYPE Selected_SERVER_PROFILE;
	static LOGINMETHOD Login_Method_TYPE;
	static String beginDATE= "1-3-2011";
	static String endDATE = "1-6-2011";
	final static String csvBaseFP ="C:\\";

	AdminTools() {

	}

	public static void main(String[] args) throws OjiException {

		// Get default locale
		//Locale locale = Locale.getDefault();
		Locale.setDefault(Locale.UK);

		System.out.println("------START ADMIN TOOL--------");
		Login_Method_TYPE = LOGINMETHOD.FromPROPERTIES;
		selectMainMenu();
		System.out.println("------END ADMIN TOOL--------\n\n");
		System.exit(1);
	}

	public static void selectMainMenu() {

		System.out.println("Please enter number: ");
		System.out.println("1.PROD   2.TEST1   3.TEST2  888.Admin   999.EXIT");

		int selected = InputHandler.getInput();
		switch (selected) {
			case 1:
				Selected_SERVER_PROFILE = SERVER_TYPE.PROD;
				break;
			case 2:
				Selected_SERVER_PROFILE = SERVER_TYPE.TEST1;
				break;
			case 3:
				Selected_SERVER_PROFILE = SERVER_TYPE.TEST2;
				break;
			case 888:
				resetConnectionPassword();
				System.exit(1);
				break;
			case 999:
				break;
			default:
				System.out.println(selected + " EXIT.");
				System.exit(1);
		}

		Comments.printServerName(Selected_SERVER_PROFILE);
		selectObjectiveMenu();

	}

	public static boolean selectObjectiveMenu() {
		boolean result = true;

		if (Selected_SERVER_PROFILE.equals(SERVER_TYPE.PROD)) {
			System.out.println("> YOU ARE ON PRODUCTION! \n");
		}

		System.out.println("____________________________________");
		System.out.println("Please Select SERVER. Enter number: ");

		System.out.println("1.Query Users 2.Query Drawings  3.Special Search  4. Delist Users  11.API Test  12. new TEST  888. Back   999.EXIT");

		int selected = InputHandler.getInput();
		switch (selected) {
			case 1:
				result = selectObjectiveQueryUserMenu();
				break;
			case 2:
				result = selectObjectiveQueryDrawingMenu();
				break;
			case 3:
				result = selectSpecialSearchMenu();
				break;
			case 4:
				result = selectDelistUserMenu();
				break;
			case 11:
				result = selectObjectiveAPITestingMenu();
				break;
			case 12:
				result = selectNewTestMenu();
				break;
			case 888:
				selectMainMenu();
				break;
			case 999:
				System.out.println(selected + " EXIT.");
				System.exit(1);
				break;

			default:
				System.out.println(selected + " Incorrect Choice!");
				result = false;
				break;
		}

		if (result != true) {
			selectMainMenu();
		}
		return result;

	}

	public static boolean selectObjectiveQueryUserMenu() {

		boolean result = true;
		LocalDB localDB;

		System.out.println("Available Options for User QUERY");
		System.out.println("________________________________");
		System.out.println("11. DB: list all users to CSV");
		System.out.println("12. DB: count # of total user");
		System.out.println("13. DB: List User Counts per Month(New & Inactivated) to CSV");
		System.out.println("14. OBJ: Search Users ");

		int selected = InputHandler.getInput();

		switch (selected) {
			case 11:
				localDB = new LocalDB(Selected_SERVER_PROFILE);
				localDB.listAllUser2CSV(csvBaseFP);
				break;

			case 12:
				localDB = new LocalDB(Selected_SERVER_PROFILE);
				result = localDB.total_users_count();
				break;

			case 13:
				localDB = new LocalDB(Selected_SERVER_PROFILE);
				result = localDB.list_user_count_per_month2CSV(csvBaseFP);
				break;

			case 14:
				Obj_Users session = new Obj_Users(myAppProperty, Selected_SERVER_PROFILE, Login_Method_TYPE);
				session.Search_SAR(beginDATE, endDATE);
				session.disconnection();
				break;

			case 888:
				selectMainMenu();
				break;

			case 999:
				System.out.println(selected + " EXIT.");
				result = true;
				System.exit(1);
				break;

			default:
				System.out.println(selected + " Incorrect Choice!");
				result = false;
				break;
		}
		if (!result) {
			selectObjectiveMenu();
		}
		return result;
	}

	public static boolean selectObjectiveQueryDrawingMenu() {

		boolean result = true;
		LocalDB localDB;

		System.out.println("Available Options for Objective DB QUERY");
		System.out.println("________________________________");
		System.out.println("21. DB: count of drawings per month to CSV");
		System.out.println("22. DB: count of total drawings");
		System.out.println("23. DB: count of total drawing versions");
		System.out.println("________________________________");
		System.out.println("41. DB: Total Audit Count");
		System.out.println("________________________________");
		System.out.println("51. List Catalogue");

		int selected = InputHandler.getInput();

		switch (selected) {


			case 21:
				localDB = new LocalDB(Selected_SERVER_PROFILE);
				result = localDB.listDrawingCountPerMonth2CSV(csvBaseFP);
				break;

			case 22:
				localDB = new LocalDB(Selected_SERVER_PROFILE);
				result = localDB.countTotalDrawingNumber();
				break;

			case 23:
				localDB = new LocalDB(Selected_SERVER_PROFILE);
				result = localDB.total_docVersion_count();
				break;

			case 41:
				localDB = new LocalDB(Selected_SERVER_PROFILE);
				result = localDB.Audit_User_Access2CSV(csvBaseFP);
				break;

			case 51:
				Obj_Search session = new Obj_Search(myAppProperty, Selected_SERVER_PROFILE, Login_Method_TYPE);
				result = session.list_catalogue();
				session.disconnection();
				break;

			case 999:
				System.out.println(selected + " EXIT.");
				result = true;
				System.exit(1);
				break;

			default:
				System.out.println(selected + " Incorrect Choice!");
				result = false;
				break;
		}
		if (!result) {
			selectObjectiveMenu();
		}
		return result;
	}

	public static boolean selectDelistUserMenu() {

		System.out.println("Available Options\n");
		System.out.println("1. delisted users: - SEARCH ONLY ");
		System.out.println("2. delisted users: - DEACTIVATE ");
		System.out.println("3. delisted users: - MOVE USER FOLDER TO X");
		System.out.println("4. delisted users: - Group: action1 (TESTING)");
		System.out.println("5. count - number of users ");
		System.out.println("999. EXIT ");
		System.out.println("	: Please enter number...	");

		int selected = InputHandler.getInput();
		Obj_Users session = new Obj_Users(myAppProperty, Selected_SERVER_PROFILE, Login_Method_TYPE);

		switch (selected) {

			case 1:
				session.Search_delisted_users_SEARCH_ONLY(myAppProperty.getProperty("DELIST_USERS"));
				break;

			case 2:
				session.Search_delisted_users_CHANGE(myAppProperty.getProperty("DELIST_USERS"));
				break;

			case 3:
				session.Search_delisted_users_MOVE_X_DRIVE(myAppProperty.getProperty("DELIST_USERS"));
				break;

			case 4:
				// String group_ID = "gA15"; // Electricity
				// session.group1(group_ID, "");
				break;

			case 999:
				session.disconnection();
				System.out.println("=== END ===");
				System.exit(1);
				break;

			default:
				System.out.println(selected + " Incorrect Choice!");
		}
		session.disconnection();
		System.out.println("Task completed");
		return false;
	}

	public static boolean selectSpecialSearchMenu() {
		boolean result = true;

		System.out.println("Available Options\n");
		System.out.println("1. Search: doc based on csv");
		System.out.println("2. Search: list drawings with attr");
		System.out.println("3. Search: list doc name with missing sheet number");
		System.out.println("4. Search: different doc name and number");
		System.out.println("5. Search: dawing count");
		System.out.println("-----------------------	");
		System.out.println("999. EXIT ");
		System.out.println("	: Please enter number...	");

		int selected = InputHandler.getInput();
		Obj_Search session;

		switch (selected) {

			case 1:
				session = new Obj_Search(myAppProperty, Selected_SERVER_PROFILE, Login_Method_TYPE);
				result = session.Search_multi_input_csv("c:\\dev\\input\\drawinglist.csv");
				session.disconnection();
				break;

			case 2:
				session = new Obj_Search(myAppProperty, Selected_SERVER_PROFILE, Login_Method_TYPE);
				result = session.search_list_doc();
				session.disconnection();
				break;

			case 3:
				session = new Obj_Search(myAppProperty, Selected_SERVER_PROFILE, Login_Method_TYPE);
				result = session.search_diff_missing_sheet_number_on_drawingname();
				session.disconnection();
				break;

			case 4:
				session = new Obj_Search(myAppProperty, Selected_SERVER_PROFILE, Login_Method_TYPE);
				result = session.search_diff_name_and_number();
				session.disconnection();
				break;

			case 5:
				session = new Obj_Search(myAppProperty, Selected_SERVER_PROFILE, Login_Method_TYPE);
				// DATE FORMAT "29-1-2006 22:01:01" or "29-1-2006"
				// session.search_drawing_count(beginDATE, endDATE);
				session.search_drawing_count();
				session.disconnection();
				result = true;
				break;

			case 999:
				System.out.println("=== END ===");
				System.exit(1);
				break;

			default:
				System.out.println(selected + " Incorrect Choice!");
		}

		System.out.println("Task completed");
		return result;
	}

	public static boolean selectNewTestMenu() {

		/*
		 * CREATE OBJECTIVE SESSION / DBMS SESSION
		 */
		System.out.println("Available Options\n");
		System.out.println("-----------------------	");
		System.out.println("1. TEST module - getUserDetails");
		System.out.println("2. TEST module - createTestUsers");
		System.out.println("3. TEST module - Audit User Access to CSV");
		System.out.println("999. EXIT ");
		System.out.println("\n: Please enter number...	");
		/*
		 * TEST ITEMS
		 */
		Obj_Users session;
		int selected = InputHandler.getInput();
		switch (selected) {

			case 1:
				session = new Obj_Users(myAppProperty, Selected_SERVER_PROFILE, Login_Method_TYPE);
				session.getUserDetails();
				session.disconnection();
				break;

			case 2:
				session = new Obj_Users(myAppProperty, Selected_SERVER_PROFILE, Login_Method_TYPE);
				session.createTestUsers(30);
				session.disconnection();
				break;

			case 3:
				LocalDB localDB = new LocalDB(Selected_SERVER_PROFILE);
				localDB.Audit_User_Access2CSV(csvBaseFP);
				break;

			case 999:
				System.out.println("=== END ===");
				System.exit(1);
				break;

			default:
				System.out.println(selected + " Incorrect Choice!");
		}

		System.out.println("Task completed");
		return false;
	}

	public static boolean selectObjectiveAPITestingMenu() {

		boolean result = true;

		System.out.println("Available Options for Objective");

		List<String> cmd = new ArrayList<String>();
		System.out.println("****** ********");
		cmd.add("1. CreateObject");
		cmd.add("2. CreateFolder");
		cmd.add("3. setObjectAttr");
		cmd.add("4. getObjectAttr");
		cmd.add("5. saveNewVersion");
		cmd.add("6. Search");
		cmd.add("7. Serach: RESERVED");
		cmd.add("8. Search: Group");
		cmd.add("9. Search: GetObjID");
		cmd.add("10. Revert");

		cmd.add("12. Get Files");

		Iterator<String> it = cmd.iterator();
		int row = 1;
		int colume = 5;
		while (it.hasNext()) {
			String value = (String) it.next();
			System.out.format("%-30s", value);
			if ((row % colume) == 0) {
				System.out.println();
			}
			row = row + 1;
		}

		/*
		 * CREATE OBJECTIVE SESSION
		 */

		Obj_API session = new Obj_API(myAppProperty, Selected_SERVER_PROFILE, Login_Method_TYPE);

		/*
		 * OBJECTIVE API TESTING
		 */
		int selected = InputHandler.getInput();
		switch (selected) {
			case 1:
				result = session.createObject();
				break;
			case 2:
				result = session.createFolder();
				break;
			case 3:
				result = session.setObjectAttr("A455904", "Line 4", "UPDATED");
				break;
			case 4:
				session.getObjectAttr();

				result = true;
				break;
			case 5:
				result = session.saveNewVersion_with_Unlink("A62004", "C:\\dev\\input\\blank_drawing.dwg",
				"TESTING. please ignore this request - Sonny Seo");
				// saveNewVersion_with_Unlink("A45508",
				// "C:\\dev\\test\\test.dwg","New Version by Sonny's miniLoader");
				break;
			case 6:
				System.out.println("Not implemeted");
				result = true;
				break;
			case 7:
				System.out.println("Not implemeted");
				break;
			case 8:
				System.out.println("Not implemeted");
				break;
			case 9:
				/*
				 * List<String> resultset_list = new ArrayList<String>();
				 * resultset_list = session.Search_GetObjID("user", "name", "is exactly", "Sonny Seo");
				 * Iterator<String> i = resultset_list.iterator();
				 * while (i.hasNext()) {
				 * String searchResult = (String) i.next();
				 * System.out.println("Id :" + searchResult);
				 * }
				 * result = true;
				 */
				break;
			case 10:
				result = session.Revert("A62006", "Comment");
				// session.saveNewVersion("A45508", "C:\\dev\\test\\test.dwg",
				// "New Version by Sonny's miniLoader");
				break;

		}
		session.disconnection();
		return result;
	}

	public static void resetConnectionPassword() {
		System.out.println("RESET PASSWORD in PROFILE: ");

		test.CryptoUtils myCryptpUtils = new test.CryptoUtils(myAppProperty.getProperty("ENCRYPTION_METHOD"));

		for (SERVER_TYPE g : SERVER_TYPE.values()) {
			System.out.println("Set Profile: " + g.toString());
			myCryptpUtils.myGenerator(g.getProfile(), myAppProperty.getProperty("KEY_FILE"));
		}
		System.out.println("RESET PASSWORD is completed. Bye. ");
	}

	protected static void timeDelay(int second) {
		try {
			Thread.sleep(1000 * second); // do nothing for 1000 miliseconds (1 second)
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
